package cn.cupk.corporateshuttlemanagesystem.mapper;

import cn.cupk.corporateshuttlemanagesystem.pojo.Companies;
import cn.cupk.corporateshuttlemanagesystem.pojo.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM users WHERE username = #{username}")
    Users findUserByUsername(String username);

    @Select("SELECT * FROM users WHERE user_id = #{userId}")
    Users findUserById(Integer userId); //根据ID查询用户

    @Select("SELECT * FROM companies WHERE name = #{companyName}")
    Companies findCompanyByName(String companyName);

    @Select("SELECT company_id FROM companies WHERE name = #{companyName}")
    Integer findCompanyIdByName(String companyName);

    @Insert("INSERT INTO users (user_id, username, password, name, email, phone, company_id, created_at, company_name) " +
            "VALUES (#{userId}, #{username}, #{password}, #{name}, #{email}, #{phone}, #{companyId}, #{createdAt}, #{companyName})")
    void addUser(Users user);

    @Update("UPDATE users set password=#{password},username = #{username} ,name=#{name},email=#{email},phone=#{phone},company_name = #{companyName},isactive = #{isactive} , company_id=#{companyId} WHERE user_id=#{userId}")
    void updateUser(Users user); //更新用户信息

    @Select({
            "<script>",
            "SELECT * FROM users",
            "<where>",
            "<if test='name != null'>",
            "AND name LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "<if test='email != null'>",
            "AND email LIKE CONCAT('%', #{email}, '%')",
            "</if>",
            "<if test='companyId != null'>",
            "AND company_id = #{companyId}",
            "</if>",
            "</where>",
            "</script>"
    })
    List<Users> page(Users users);



    @Select("SELECT * FROM companies WHERE company_id = #{companyId}")
    Companies findCompanyById(int companyId);
}


